//
//  HomeHeaderView.m
//  兼职页面
//
//  Created by kuiyuan liang on 2021/5/29.
//

#import "HomeHeaderView.h"
#import "CommonUtil.h"
@interface HomeHeaderView()
@property (strong, nonatomic) UIImageView * bgImageView;
@property (strong, nonatomic) UIView * whiteView;
@property (strong, nonatomic) UILabel * titleLabel;
@property (strong, nonatomic) UIImageView * iconImageView;


@property (strong, nonatomic) UILabel * nameLabel;
@property (strong, nonatomic) UILabel * subTitleLabel;

@property (strong, nonatomic) UIView * whiteView2;
@property (strong, nonatomic) UIScrollView * tabView;
@property (strong, nonatomic) UIButton * selectBtn;
@property (strong, nonatomic) UIButton * moreBtn;

@end

@implementation HomeHeaderView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
       
        
        [self addSubview:self.bgImageView];
        [self addSubview:self.backBtn];
        [self addSubview:self.titleLabel];
        [self addSubview:self.searchBtn];
        [self addSubview:self.whiteView];
        [self.whiteView addSubview:self.iconImageView];
        [self.whiteView addSubview:self.wordBtn];
        [self.whiteView addSubview:self.blacklistBtn];
        [self.whiteView addSubview:self.nameLabel];
        [self.whiteView addSubview:self.subTitleLabel];
        
        
        CGFloat w = self.whiteView.width/4;
        for (NSInteger i = 0; i < 4; i++) {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(i * w, self.subTitleLabel.bottom , w, AD(50));
            btn.titleLabel.numberOfLines = 0;
            btn.titleLabel.font = [UIFont systemFontOfSize:AD(13)];
            btn.titleLabel.textAlignment = NSTextAlignmentCenter;
            [btn setTitleColor:RGB(153.0, 153.0, 153.0) forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(followBtnClick:) forControlEvents:UIControlEventTouchUpInside];
            [self.whiteView addSubview:btn];
            if (i == 0) {
                [btn setTitle:@"63\n关注专家" forState:UIControlStateNormal];
            }else if (i == 1){
                [btn setTitle:@"552\n关注粉丝" forState:UIControlStateNormal];
            }else if (i == 2){
                [btn setTitle:@"91\n发布帖子" forState:UIControlStateNormal];
            }else if (i == 3){
                [btn setTitle:@"3098\n获赞数" forState:UIControlStateNormal];
            }
        }
        
        
        [self addSubview:self.whiteView2];
        [self.whiteView2 addSubview:self.tabView];
        [self.whiteView2 addSubview:self.moreBtn];
        NSArray *btnTitles = @[@"高手论坛",@"港彩名人堂",@"澳彩名人堂",@"猛料市场",@"新加坡彩",@"澳彩高手",@"台湾彩",@"时时彩"];
        CGFloat x = 0;
        for (NSInteger i = 0; i < btnTitles.count; i++) {
            NSString *title = btnTitles[i];
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.titleLabel.font = [UIFont systemFontOfSize:AD(13)];
            [btn setTitle:title forState:UIControlStateNormal];
            CGFloat w = [CommonUtil countWidth:title font:btn.titleLabel.font height:self.tabView.height] + AD(15);
            btn.frame = CGRectMake(x, 0, w, self.tabView.height);
            x = btn.right;
            [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [btn setTitleColor:RGB(255.0, 89.0, 117.0) forState:UIControlStateSelected];
            [btn addTarget:self action:@selector(tabBtnClick:) forControlEvents:UIControlEventTouchUpInside];
            [self.tabView addSubview:btn];
            if (i == 0) {
                [self tabBtnClick:btn];
            }
            self.tabView.contentSize = CGSizeMake(btn.right, 0);
        }
        
        
        
    }
    return self;
}

- (void)followBtnClick:(UIButton *)sender{
    NSLog(@"%@",sender.titleLabel.text);
}

- (void)tabBtnClick:(UIButton *)sender{
    NSLog(@"%@",sender.titleLabel.text);
    self.selectBtn.selected = false;
    self.selectBtn = sender;
    self.selectBtn.selected = YES;
    if (self.actionBlock) {
        self.actionBlock(sender.titleLabel.text);
    }
}
- (void)moreBtnClick:(UIButton *)sender{
    sender.selected = !sender.selected;
    NSLog(@"%@",sender.titleLabel.text);
    
//    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 200, 300, 500)];
//    [self addSubview:v];
//    v.backgroundColor = [UIColor lightGrayColor];
    
}


-(UIImageView *)bgImageView{
    if (!_bgImageView) {
        _bgImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"forum_bg.jpg"]];
        _bgImageView.frame = CGRectMake(0, 0, SCREEN_WIDTH, AD(250));
        _bgImageView.userInteractionEnabled = YES;
    }
    return _bgImageView;
}

-(UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(AD(80), _backBtn.top, SCREEN_WIDTH-AD(160), AD(30))];
        _titleLabel.textColor = [UIColor whiteColor];
        _titleLabel.font = [UIFont systemFontOfSize:AD(18)];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.text = @"用户主页";
    }
    return _titleLabel;
}

-(UIButton *)backBtn{
    if (!_backBtn) {
        _backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_backBtn setImage:[UIImage imageNamed:@"icon_back"] forState:UIControlStateNormal];
        [_backBtn setTitle:@"返回" forState:UIControlStateNormal];
        [_backBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _backBtn.frame = CGRectMake(AD(10), kStatusBarHeight + AD(7), AD(60), AD(30));
        _backBtn.imageEdgeInsets = UIEdgeInsetsMake(0, AD(-15), 0, 0);
    }
    return _backBtn;
}

-(UIButton *)searchBtn{
    if (!_searchBtn) {
        _searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_searchBtn setImage:[UIImage imageNamed:@"icon_home_share"] forState:UIControlStateNormal];
        [_searchBtn setTitle:@"分享" forState:UIControlStateNormal];
        [_searchBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _searchBtn.frame = CGRectMake(SCREEN_WIDTH-AD(80), 0, AD(60), AD(30));
        _searchBtn.centerY = self.backBtn.centerY;
    }
    return _searchBtn;
}


-(UIView *)whiteView{
    if (!_whiteView) {
        _whiteView = [[UIView alloc] initWithFrame:CGRectMake(AD(10), AD(150), SCREEN_WIDTH-AD(20),AD(170))];
        _whiteView.backgroundColor = [UIColor whiteColor];
        _whiteView.layer.cornerRadius = AD(7);
    }
    return _whiteView;
}

-(UIImageView *)iconImageView{
    if (!_iconImageView) {
        _iconImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"statistics_default.jpg"]];
        _iconImageView.frame = CGRectMake(AD(10), AD(-20), AD(70), AD(70));
        _iconImageView.layer.cornerRadius = AD(35);
        _iconImageView.clipsToBounds = YES;
    }
    return _iconImageView;
}

-(UIButton *)wordBtn{
    if (!_wordBtn) {
        _wordBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_wordBtn setTitle:@"留言" forState:UIControlStateNormal];
        [_wordBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        _wordBtn.titleLabel.font = [UIFont systemFontOfSize:AD(13)];
        [_wordBtn setImage:[UIImage imageNamed:@"icon_redword"] forState:UIControlStateNormal];
        _wordBtn.backgroundColor = RGB(247.0, 247.0, 247.0);
        _wordBtn.layer.borderWidth = AD(0.5);
        _wordBtn.layer.borderColor = [UIColor lightGrayColor].CGColor;
        _wordBtn.frame = CGRectMake(_iconImageView.right + AD(15), AD(12), AD(110),AD(30));
        _wordBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -AD(5));
    }
    return _wordBtn;
}

-(UIButton *)blacklistBtn{
    if (!_blacklistBtn) {
        _blacklistBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_blacklistBtn setTitle:@"黑名单" forState:UIControlStateNormal];
        [_blacklistBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        
        _blacklistBtn.titleLabel.font = [UIFont systemFontOfSize:AD(13)];
        [_blacklistBtn setImage:[UIImage imageNamed:@"icon_blacklist"] forState:UIControlStateNormal];
        _blacklistBtn.backgroundColor = RGB(247.0, 247.0, 247.0);
        _blacklistBtn.layer.borderWidth = AD(0.5);
        _blacklistBtn.layer.borderColor = [UIColor lightGrayColor].CGColor;
        _blacklistBtn.frame = CGRectMake(_wordBtn.right + AD(10), AD(12), AD(110),AD(30));
        _blacklistBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -AD(5));
    }
    return _blacklistBtn;
}

-(UILabel *)nameLabel{
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(AD(10), _iconImageView.bottom + AD(15), AD(200), AD(25))];
        _nameLabel.font = [UIFont systemFontOfSize:AD(18)];
        _nameLabel.textColor = [UIColor blackColor];
        _nameLabel.text = @"合家欢喜";
    }
    return _nameLabel;
}


-(UILabel *)subTitleLabel{
    if (!_subTitleLabel) {
        _subTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(AD(10), _nameLabel.bottom + AD(5), AD(200), AD(25))];
        _subTitleLabel.font = [UIFont systemFontOfSize:AD(13)];
        _subTitleLabel.textColor = [UIColor lightGrayColor];
        _subTitleLabel.text = @"这个人很懒,他什么也没说...";
    }
    return _subTitleLabel;
}

-(UIView *)whiteView2{
    if (!_whiteView2) {
        _whiteView2 = [[UIScrollView alloc] initWithFrame:CGRectMake(0, self.whiteView.bottom + AD(10), SCREEN_WIDTH,AD(50))];
        _whiteView2.backgroundColor = [UIColor whiteColor];
    }
    return _whiteView2;
}

-(UIScrollView *)tabView{
    if (!_tabView) {
        _tabView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH-AD(40),self.whiteView2.height)];
    }
    return _tabView;
}
-(UIButton *)moreBtn{
    if (!_moreBtn) {
        _moreBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_moreBtn addTarget:self action:@selector(moreBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [_moreBtn setImage:[UIImage imageNamed:@"icon_home_more"] forState:UIControlStateNormal];
        [_moreBtn setImage:[UIImage imageNamed:@"icon_chacha_small"] forState:UIControlStateSelected];
        _moreBtn.frame = CGRectMake(SCREEN_WIDTH-AD(40), AD(10), AD(30),AD(30));
    }
    return _moreBtn;
}
@end
